Repository navigation
feat: warn when Java calls a non-class method - #2139
Draft
edusperoni wants to merge 4 commits into
Draft
edusperoni wants to merge 4 commits into
edusperoni wants to merge 4 commits into
Conversation
Java->JS calls look the method up on the JS instance per call, so an own
property of the instance (a per-instance override, a spy, a TypeScript
class-field arrow function) or a method replaced after extend() is what
runs. The iOS runtime binds a class's methods when it is extended and
never sees either. Before deciding whether to align Android with iOS, we
want to know how often apps rely on this.
In debug builds (APPLICATION_IN_DEBUG), each extend() now captures the
implementation object's own function-valued data properties. The class's
record is reached from the wrapper's link state, which the call path
already fetches, and it is keyed lazily by Java's method-name id. A call
that resolves to a different function than the captured one is logged
once per class and method under the JS tag, with the location of the
function that ran. For a name the class does not define, the first call
resolves to whatever the class inherits, unless the instance, its
prototype or the implementation object owns the name.
Behaviour is unchanged: the call still goes to the function it reaches
today. Release builds (nativescript-optimized) compile none of it.
Instances of `new SomeInterface({...})` are not checked yet: their
implementation object is per instance, so a per-class record does not
apply to them.
Specs for a class's own method (no warning), an instance own property, a TypeScript class field, a method replaced on the implementation object and one added to the class prototype after extend(), a Java-created instance, and repeated calls on several instances (one warning). Each spec checks the JS function Java reached and counts the warning in logcat through a LogcatUtil helper that leaves the log in place.
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
The log outlives the app, so a second run on the same device counted the warnings of the first. Lines are now matched to android.os.Process.myPid(); logcat's --pid filter needs API 24.
`new SomeInterface({...})` gives every instance its own implementation
object, so it has no per-class record to compare against. For wrappers
without a record, debug builds now check whether the receiver itself owns
the method name and, if so, log the same warning once per Java class and
method per runtime. This costs one own-property lookup per Java->JS call
on those instances, in debug builds only; records keep the pointer
compare.
ObjectManager::GetJavaClass now returns null for an object without a
link instead of dereferencing a null info.
Specs: a plain interface literal called from Java does not warn; one whose
instance gets its own `run` still runs it and warns once over two calls.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Current behaviour. A Java→JS call looks the method up on the JS instance on every call (
jsObject->Get(context, methodName)inCallbackHandlers::CallJSMethod). So whatever the instance reaches at call time runs:listener.onClick = …), a test spy, or a TypeScript class-field arrow function (onClick = (v) => …in a@NativeClassclass);extend().The iOS runtime binds a class's methods when the class is extended and never sees either case. Before deciding whether Android should match iOS, this PR measures how much real apps depend on the current behaviour.
What this adds (debug builds only, no behaviour change).
Capture.
extend()records the implementation object's own function-valued data properties (DispatchOverrides, one per class). It reads property descriptors, so no getter runs. The record is owned by the runtime'sMetadataNodeCache, so each worker isolate has its own, and itsv8::Globals are released at runtime teardown while the isolate is still alive.Lookup key.
ObjectManager::Linkstores a pointer to the class's record in the wrapper's link state. Java→JS calls already fetch that state to find the JS object, so getting the record costs one field load. This covers instances made by the JS constructor, by the TypeScript__extendspath and by Java (CreateJSInstanceNative). Inside the record, entries are indexed by Java's method-name id (MethodNameIds). Those ids are assigned on Java's first call with each name, so each class+method entry is bound by name on its first call.Check. After the lookup, the function the call reached is compared with the captured one (handle identity). On a mismatch the runtime logs this once per class and method under the
JStag (logcat):If the class doesn't define the name at all, the first call is accepted as the class's own implementation when it resolves to something the class inherits. It warns only if the instance, its prototype or the implementation object owns the name; that is the class-field-only case.
No JS runs during the check. It doesn't call
console.warn, because a JS call at depth 0 would drain microtasks before the dispatched method runs. It doesn't throw either. After the check, the call still goes to the function it reaches today.Cost.
nativescript-optimized, noAPPLICATION_IN_DEBUG): the capture, the per-wrapper link-state field and the check are all behind#ifdef. What remains is two extra arguments passed throughCallJSMethod, which release builds ignore, and two pointer fields in the per-class cache structs that are always null.nativescript-optimized-with-inspector,-O3), per Java→JS call: one load of the record pointer from the link state the call already holds, then a null check, a bounds check, a flag test and a handle comparison. There is no extra property lookup and no call. In the arm64 disassembly ofCallJSMethod, the inlined check is 17 instructions (6 loads, 6 well-predicted branches). The slow path runs once per class+method.new SomeInterface({...}), debug builds only): one own-property lookup on the receiver per Java→JS call.new SomeInterface({...})instances. Each of these gets its own implementation object as its prototype, so there's no per-class record to compare against. For any wrapper without a record, debug builds instead check whether the receiver itself owns the method name (HasRealNamedProperty, which runs neither interceptors nor getters). If it does, the runtime logs the same warning once per Java class and method in each runtime. A function reassigned on the implementation object itself isn't detected on these instances.Goal. Collect reports from real apps that hit this warning before deciding whether to bind methods at extend time, as iOS does.
Does your commit message include the wording below to reference a specific issue in this repo?
No.
Related Pull Requests
None.
Does your pull request have unit tests?
Yes.
testDispatchOverrideWarnings.jscovers these cases:@Interfaces): called, warns;extend(): warns;extend(): warns;new java.lang.Runnable({ run }): called, no warning;r.run = …afterwards: the own property is called, and two calls give one warning;Each spec checks that Java actually reached the JS function. It counts warnings with a new
LogcatUtil.countJsWarnings, which readslogcat -d -s JS:Wwithout clearing the log and counts only this process's lines; the existinglogcatContainsStringclears the log first. The branch for a name the class doesn't define but inherits has no spec: I couldn't build a binding that routes such a name to JS. The warning goes to logcat only, not to the DevTools console.